package data;

import java.util.Date;
import java.util.HashMap;
import java.util.Vector;

public class Member extends User {
	private Vector<Member> _friends;
	private Boolean _activeState;
	private Vector<ForumMessageUserObject> _messages;
	private Boolean _isAdmin;
	private HashMap<Integer, Date> _forumModerator;
	private Date _lastMsgPublish;
	
	public Member(String _name, Integer _id) {
		super(_name, _id);
		this._friends = new Vector<Member>();
		this._activeState = true;
		_messages = new Vector<ForumMessageUserObject>();
		this._isAdmin = false;
		this._forumModerator=new HashMap<Integer, Date>();
		_lastMsgPublish=null;
	}
	

	public Member(String name, Integer id, Vector<Member> friends, Vector<ForumMessageUserObject> messages, Boolean activeState) {
		super(name, id);
		this._friends = friends;
		this._activeState = activeState;
		this._messages = messages;
		this._isAdmin = false;
		this._forumModerator=new HashMap<Integer, Date>();
		this._lastMsgPublish=null;
	}


	public Vector<Member> get_friends() {
		return _friends;
	}

	public Boolean get_active() {
		return _activeState;
	}

	public void set_active(Boolean _active) {
		this._activeState = _active;
	}
	
	
	public Date get_lastMsgPublish() {
		return _lastMsgPublish;
	}


	public void set_lastMsgPublish(Date _lastMsgPublish) {
		this._lastMsgPublish = _lastMsgPublish;
	}


	public void addFriend(Member friend){
		if (!_friends.contains(friend))
			_friends.add(friend);
	}
	
	public void removeFriend(Member friend){
		if (_friends.contains(friend))
			_friends.remove(friend);
	}
	@Deprecated
	public void addMessage(Integer forumId, Integer subForumId , Integer msgId){
		boolean found=false;
		for(ForumMessageUserObject it: _messages)
		{
			if( it.get_subForumId()== subForumId &&	it.get_msgId()==msgId){
				found=true;
			}
		}
		if(!found)
			_messages.add(new ForumMessageUserObject( subForumId, msgId));
	}
	
	public void addMessage( Integer subForumId , Integer msgId ){
		boolean found=false;
		for(ForumMessageUserObject it: _messages)
		{
			if( it.get_subForumId()== subForumId &&	it.get_msgId()==msgId){
				found=true;
			}
		}
		if(!found)
			_messages.add(new ForumMessageUserObject( subForumId, msgId));		
	}
	@Deprecated
	public boolean removeMessage(Integer forumId, Integer subForumId , Integer msgId){
		for(ForumMessageUserObject it: _messages){
			if( it.get_subForumId()== subForumId &&
					it.get_msgId()==msgId)
			{
				_messages.remove(it);
				return true;
			}
		}
		return false;
	}
	
	public boolean removeMessage( Integer subForumId , Integer msgId){
		for(ForumMessageUserObject it: _messages){
			if( it.get_subForumId()== subForumId &&
					it.get_msgId()==msgId)
			{
				_messages.remove(it);
				return true;
			}
		}
		return false;
	}
	
	public Boolean get_isAdmin() {
		return _isAdmin;
	}


	public void set_isAdmin(Boolean _isAdmin) {
		this._isAdmin = _isAdmin;
	}
	
	public boolean isModerator(){
		return this._forumModerator.isEmpty();
	}
	
	public boolean isModerator(Integer subForumId){
		return this._forumModerator.containsKey(subForumId);
	}
	
	public void addSubforumAsModerator(Integer subForumId){
		if(!_forumModerator.containsKey(subForumId))
			this._forumModerator.put(subForumId, new Date(System.currentTimeMillis()));
	}
	
	public boolean removeSubforumAsModerator(Integer subForumId){
	 if (!this._forumModerator.containsKey(subForumId))
		 return false;
	this._forumModerator.remove(subForumId);
	return true;
	}
	
	public Date getStartDateAsModerator(Integer subForumID){
		if(isModerator(subForumID))
			return this._forumModerator.get(subForumID);
		return null;
	}


	public boolean ownMessage( Integer subForumID,Integer messageID) {
		for(ForumMessageUserObject msg : _messages)
			if(msg.get_subForumId().equals(subForumID) && msg.get_msgId().equals(messageID))
				return true;
		return false;
	}
}
